package com.bra.ui.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bra.R;
import com.bra.bean.ArticleBean;
import com.bra.request.UrlConstants;
import com.bra.ui.adapter.VenueList_adapter;
import com.bra.ui.adapter.SelectVenue_time_adapter;
import com.bra.ui.adapter.SelectVenue_advance_adapter;
import com.bra.ui.adapter.ArticleList_adapter;
import com.bra.ui.base.BaseActivity;
import com.bra.utils.CommonUtils;
import com.bra.utils.SPHelper;
import com.bra.view.ColumnHorizontalScrollView;
import com.bra.view.Hsv_selectVenue_advance;

/**
 * 选择场地
 * 
 * @author lenovo
 *
 */
public class SelectVenueActivity extends BaseActivity {
	private TextView activity_title;
	private ImageView activity_back;
	private ColumnHorizontalScrollView mColumnHorizontalScrollView;
	private RelativeLayout rl_column;
	private LinearLayout mRadioGroup_content;
	/** 屏幕宽度 */
	private int mScreenWidth = 0;
	/** Item宽度 */
	private int mItemWidth = 0;
	private String[] items = new String[7];
	private String[] adapteritems = new String[16];
	private String[] timeitems = new String[16];
	private int columnSelectIndex = 0;
	/** 左阴影部分 */
	public ImageView shade_left;
	/** 右阴影部分 */
	public ImageView shade_right;
	private Hsv_selectVenue_advance horizon_listview;
	private SelectVenue_advance_adapter yd_adapter;
	private ListView xzcd_list_time;
	private SelectVenue_time_adapter time_adapter;
	private TextView columnTextView;
	private RequestQueue mRequestQueue;
	private String userid, token;
	private String Select_XqUrl = UrlConstants.IP;
	private Intent intent;
	private String cgid,qiuleiid;
	@Override
	protected View onCreateView(LayoutInflater inflater,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.activity_selectvenue, null);
	}

	@Override
	protected void onViewCreated(Bundle savedInstanceState) {
		userid = SPHelper.readString(this, "userId");
		token = SPHelper.readString(this, "token");
		intent=getIntent();
		cgid=intent.getStringExtra("cgid");
		qiuleiid=intent.getStringExtra("qiuleiid");
		mRequestQueue = Volley.newRequestQueue(this);
		mScreenWidth = CommonUtils.getWindowsWidth(this);
		mItemWidth = mScreenWidth / 5;// 一个Item宽度为屏幕的1/7
		activity_title = (TextView) findViewById(R.id.activity_title);
		activity_title.setText("选择场地");
		activity_back = (ImageView) findViewById(R.id.activity_back);
		mColumnHorizontalScrollView = (ColumnHorizontalScrollView) findViewById(R.id.mColumnHorizontalScrollView);
		mRadioGroup_content = (LinearLayout) findViewById(R.id.mRadioGroup_content);
		shade_left = (ImageView) findViewById(R.id.shade_left);
		shade_right = (ImageView) findViewById(R.id.shade_right);
		rl_column = (RelativeLayout) findViewById(R.id.rl_column);
		horizon_listview = (Hsv_selectVenue_advance) findViewById(R.id.horizon_listview);	
		xzcd_list_time = (ListView) findViewById(R.id.xzcd_list_time);

		for (int i = 0; i < items.length; i++) {

			items[i] = "今天01-0" + i;
		}
		 initTabColumn();
		for (int i = 0; i < adapteritems.length; i++) {

			adapteritems[i] = i + "号场地";
		}
		for (int i = 0; i < timeitems.length; i++) {
            if(i>=10){
            	timeitems[i] = i + ":00";    
            }else{
            	timeitems[i] = "1" + i + ":00";            }
			
		}
			
		if (yd_adapter == null) {
			yd_adapter = new SelectVenue_advance_adapter(SelectVenueActivity.this,
					adapteritems);
		}
		horizon_listview.setAdapter(yd_adapter);
		if (time_adapter == null) {
			time_adapter = new SelectVenue_time_adapter(SelectVenueActivity.this,
					timeitems);
		}
		xzcd_list_time.setAdapter(time_adapter);	
		httpRequestSelectCGInfo(SelectVenueActivity.this,Select_XqUrl,RequestParams(userid,token,cgid,""));
	}

	/**
	 * 初始化Column栏目项
	 * */
	private void initTabColumn() {

		mRadioGroup_content.removeAllViews();
		int count = items.length;
		mColumnHorizontalScrollView.setParam(this, mScreenWidth,
				mRadioGroup_content, shade_left, shade_right, rl_column);
		for (int i = 0; i < count; i++) {
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					mItemWidth, LayoutParams.WRAP_CONTENT);
			params.rightMargin = 4;
			params.leftMargin = 4;
			columnTextView = new TextView(this);
			columnTextView.setTextAppearance(this, R.style.xzcd_top_text);
		//	columnTextView.setBackgroundResource(R.drawable.radio_buttong_bg);
			columnTextView.setGravity(Gravity.CENTER);
			columnTextView.setPadding(5, 5, 5, 5);
			columnTextView.setId(i);
			columnTextView.setText(items[i]);
			columnTextView.setTextColor(getResources().getColorStateList(R.color.xzcd_title_textcolor));

			if (columnSelectIndex == i) {
				columnTextView.setSelected(true);
			}
			columnTextView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					for (int i = 0; i < mRadioGroup_content.getChildCount(); i++) {
						View localView = mRadioGroup_content.getChildAt(i);
						if (localView != v)
							localView.setSelected(false);
						else {
							localView.setSelected(true);
						//	init_content(i);
							Toast.makeText(getApplicationContext(), items[i],
									Toast.LENGTH_SHORT).show();							
						}
					}					
				}
			});
			mRadioGroup_content.addView(columnTextView, i, params);
		}
	}

	@Override
	protected void setListener() {
		activity_back.setOnClickListener(click);

	}

	private View.OnClickListener click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.activity_back:
				finish();
				break;
			default:
				break;
			}

		}
	};
	private void init_content(int i){		
			adapteritems[i] = i + "场地";		
		if (yd_adapter == null) {
			yd_adapter = new SelectVenue_advance_adapter(SelectVenueActivity.this,
					adapteritems);
		}
		horizon_listview.setAdapter(yd_adapter);
		//yd_adapter.notifyDataSetChanged();
	}
	
	private void httpRequestSelectCGInfo(Context context, String url,
			final HashMap<String, String> params) {
		StringRequest req = new StringRequest(Request.Method.POST, url,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						try {	
						 System.out.println(response.toString() + "===");
									
							return;
						} catch (Exception e) {
							e.printStackTrace();
						}
						Toast.makeText(SelectVenueActivity.this, "数据异常，请检查网络", 0).show();
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError volleyError) {
						Toast.makeText(SelectVenueActivity.this, "访问失败，请检查网络", 0).show();
					}
				}) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				Map<String, String> map = params;
				return map;
			}
		};
		mRequestQueue.add(req);
	}

	private HashMap<String, String> RequestParams(String userId,String token,String cgid,String typeid) {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("userId", userId);
		map.put("equipment", "android");
		map.put("requestType", "Select_Field_Api");
		map.put("token", token);
		map.put("venueId", cgid);
		map.put("venueId", typeid);
		return map;
	}
}
